#include "uart_drive.h"
#include "usart.h"
#include <stdio.h>
#include "w25q64_drive.h"

#define DEBUG_UART_TIMEOUT 500
#define UART_SIZE 256

uint8_t rx_flag = 0;
uint8_t rx_buff1[UART_SIZE],rx_buff2[UART_SIZE];
uint8_t *p_tx_buff = rx_buff1;
uint8_t *p_rx_buff = rx_buff2;
uint8_t my_spi[UART_SIZE];
uint32_t addr_write = 0x000000;

extern UART_HandleTypeDef huart1;
static UART_HandleTypeDef * g_HDebugUART = &huart1;

int fputc(int c, FILE *f) 
{
    (void)f;
    HAL_UART_Transmit(g_HDebugUART, (const uint8_t *)&c, 1, DEBUG_UART_TIMEOUT);
    return c;
}

void UART_Init(void)
{
}


void UART_Config(void)
{
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1, p_rx_buff, UART_SIZE);
    __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);   //关闭接收一半的中断
}



//双缓冲区dma收发数据
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
    if(huart == &huart1)
    {   
        if(rx_flag == 0)
        {
            p_tx_buff = rx_buff2;
            p_rx_buff = rx_buff1;
            rx_flag = 1;
        }
        else
        {
            p_tx_buff = rx_buff1;
            p_rx_buff = rx_buff2;
            rx_flag = 0;
        }
        W25Q64_PageProgram(addr_write, p_tx_buff, UART_SIZE);
        W25Q64_ReadData(addr_write, my_spi, UART_SIZE);
        HAL_UART_Transmit_DMA(&huart1, my_spi, UART_SIZE);
        addr_write += 256;
        UART_Config();
    }
}



